package com.threeox.commonlibrary.util.request;

import com.alibaba.fastjson.JSON;
import com.threeox.commonlibrary.CommonApplcation;
import com.threeox.commonlibrary.entity.engine.request.sharedpre.OperationTypeMsg;
import com.threeox.commonlibrary.entity.engine.request.sharedpre.SharedPreReqMsg;
import com.threeox.commonlibrary.util.sql.TableSqlExecutor;
import com.threeox.ormlibrary.impl.SqlExecutor;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;
import com.threeox.utillibrary.util.file.SharedPreferencesUtil;

import java.io.Serializable;
import java.util.List;

/**
 *
 * @ClassName: SqlIteRequestHelper
 *
 * @Description: Todo(本地SqlIte的请求帮助类)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2017/8/6 下午11:08
 * 
 * @version 1.0
 */
public class SharedPreRequestHelper {

    private final String TAG = SharedPreRequestHelper.class.getName();

    private SqlExecutor mSqlExecutor = null;
    private TableSqlExecutor mTableSqlExecutor = null;

    private CommonApplcation mApplcation;
    private RequestHelper mRequestHelper;

    private SharedPreRequestHelper(){}

    private SharedPreRequestHelper(RequestHelper requestHelper){
        this.mRequestHelper = requestHelper;
        this.mApplcation = CommonApplcation.getInstance();
    }

    public static SharedPreRequestHelper newInstance(RequestHelper requestHelper) {
        return new SharedPreRequestHelper(requestHelper);
    }

    /**
     * 请求SqlIte 本地数据库
     *
     * @param requestMsg
     */
    public void execSharedPre(SharedPreReqMsg requestMsg) {
        if (requestMsg == null)
            return;
        try {
            SharedPreferencesUtil preferences;
            String name = requestMsg.getName();
            if (EmptyUtils.isNotEmpty(name)) {
                preferences = SharedPreferencesUtil.newInstance(mApplcation, name, requestMsg.getPreferencesMode());
            } else {
                preferences = SharedPreferencesUtil.newInstance(mApplcation, requestMsg.getPreferencesMode());
            }
            OperationTypeMsg operationType = requestMsg.getOperationType();
            if (OperationTypeMsg.GET == operationType) {
                execGetSharePre(requestMsg, preferences);
            } else if (OperationTypeMsg.PUT == operationType) {

            } else if (OperationTypeMsg.REMOVE == operationType) {

            }
        } catch (Exception e) {
            LogUtils.e(TAG, e.getMessage());
            mRequestHelper.sendErrorResult(requestMsg, requestMsg.getKey(), e, e.getMessage());
        }
    }

    /**
     * 取得SharedPreferences 里面的数据
     *
     * @param requestMsg
     * @param preferences
     * @throws Exception
     */
    public void execGetSharePre(SharedPreReqMsg requestMsg, SharedPreferencesUtil preferences) throws Exception {
        Object result = preferences.get(requestMsg.getKey(), null);
        if (result != null && result instanceof String) {
            try {
                Object data = JSON.parseObject(result.toString(), requestMsg.getResultClass());
                mRequestHelper.sendSuccessResult(requestMsg, requestMsg.getKey(), (Serializable) data);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getLocalizedMessage());
                List datas = JSON.parseArray(result.toString(), requestMsg.getResultClass());
                mRequestHelper.sendSuccessResult(requestMsg, requestMsg.getKey(), (Serializable) datas);
            }
        } else {
            mRequestHelper.sendSuccessResult(requestMsg, requestMsg.getKey(), (Serializable) result);
        }
    }

}
